﻿using log4net;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HuChaHuRen.HeNan.XiangCheng.Common
{
    public class DataProvider
    {
        private static string connectionString = ConfigHelp.GetBaseConfig().PACSConnStr;
        private static ILog logger = LogManager.GetLogger("DataProvider");

        public static SqlSugarClient GetNewConn()
        {
            if(string.IsNullOrEmpty(connectionString))
            {
                logger.Error($"数据库连接字符串为空(PACSConnStr).");
                return null;
            }
            else
            {
                string rawConnStr = "";
                try
                {
                    rawConnStr = Crypto.DESDecrypt(connectionString);
                }
                catch (Exception ex)
                {
                    logger.Error($"数据库连接字符串(PACSConnStr)解密失败.密文为:\n'{connectionString}'.\n错误信息为:{ex.Message}");
                    return null;
                }

                return new SqlSugarClient(new ConnectionConfig
                {
                    ConnectionString = rawConnStr,
                    DbType = DbType.SqlServer,
                    IsAutoCloseConnection = true,
                    InitKeyType = InitKeyType.SystemTable
                });
            }
        }
    }
}
